const CHARACTER_STATUS = { STAND:0, 
                           RUNNING:1,
                           WALKING:2 };
const CHARACTER_ANIMATION = {RUNNING:0,
                             SITTING:1,
                             WALKING:2,
                             STANDING1:3,
                             SMOKING:4};

const GIRL_ANIMATION = {STANDING:0,
                        RUNNING:1};

const CHARACTER_RUN_SPD = 5;
//const CHARACTER_RUN_SPD = 20;
const CHARACTER_WALK_SPD = 2;
function Character(screen,node,heightmap,physicbody)
{
  this.screen = screen;
  this.name = "";
  this.node = node;
  this.heightMap = heightmap;
  this.startPosition = new gewVec2();
  this.position = new gewVec2();
  this.desPosition = new gewVec2();
  this.status = CHARACTER_STATUS.STAND;
  this.speed = 0; // m/s
  this.velocity = new gewVec2();
  this.physicBody = physicbody;
  this.physicBody.userData = this;
  this.waypoints = null;
  this.currentPointOffset = 0;
  this.isReapeat = true;
  this.isGirl = false;
  this.isCollide = false;
  this.isPreviousCollide = false;
  this.collideNormal = new gewVec2();
}
Character.prototype = {
  onTick:function(mls)
  {
    if(this.status== CHARACTER_STATUS.RUNNING || this.status == CHARACTER_STATUS.WALKING)
    {      
      if (this.isCollide) {
        this.isCollide = false;
        var angle = this.collideNormal.angleFrom(this.velocity)* GEW_MATH_RADIAN_TO_DEGREE;
        if(angle>80)
        {
          this.stand();
          return;
        }
      }
      var s = mls/1000.;
      var currPos = this.position;      
      var delta1 = this.desPosition.sub(this.startPosition).getLengthSquare();
      var delta2 = currPos.sub(this.startPosition).getLengthSquare();
       
      var quangduong = this.velocity.mulScalar(s);
      if( delta2>=delta1)
      {
        quangduong.set(0,0);
        if( !this.waypoints)
          this.stand();
        else
        {
          this.currentPointOffset++;
          if (this.currentPointOffset >= this.waypoints.length/2) {
            if (this.isReapeat) 
              this.currentPointOffset = -1;
            else 
              this.stand();
          }
          else {
            if(this.status == CHARACTER_STATUS.RUNNING)
              this.moveTo(this.waypoints[this.currentPointOffset*2],this.waypoints[this.currentPointOffset*2+1]);
            else if( this.status==CHARACTER_STATUS.WALKING)
              this.walkTo(this.waypoints[this.currentPointOffset*2],this.waypoints[this.currentPointOffset*2+1]);
          }  
        }    
      }
      var newPos = currPos.add(quangduong);    
      this.setPositionXY(newPos.x,newPos.y);      
    }   
  },
  setPositionXY:function(x,y)
  {
    this.position.set(x,y);
    var height = this.heightMap.interpolationAt(x,y);
    this.node.setPositionXYZ(x,height,y);
  },
  stand:function()
  {
    this.status = CHARACTER_STATUS.STAND;
    this.node.animationSpeed = 1.3;
    if(!this.isGirl)
      this.node.setAnimation( CHARACTER_ANIMATION.STANDING1 );
    else
      this.node.setAnimation( GIRL_ANIMATION.STANDING );
    this.node.play(false);
    
    if (this == my_player) {
      var pos = this.node.position;
      var str = "1\r\n" + this.name + "\r\n" + pos.x + ";" + pos.y + ";" + pos.z;
      this.screen.socket.send(str);
    }    
  },
  moveAlongWaypoints:function(waypoints,repeat)
  {
    this.isReapeat = repeat;
    this.waypoints = waypoints;
    this.currentPointOffset = 0;
    this.moveTo(waypoints[0],waypoints[1]);
  },
  walkAlongWaypoints:function(waypoints,repeat)
  {
    this.isReapeat = repeat;
    this.waypoints = waypoints;
    this.currentPointOffset = 0;
    this.walkTo(waypoints[0],waypoints[1]);
  },
  walkTo:function(x,y)
  {    
    var currPos = this.position;
    this.speed = CHARACTER_WALK_SPD;
    this.startPosition.set(currPos.x,currPos.y);
    this.desPosition.set(x,y);
    this.velocity = this.desPosition.sub(currPos);
    this.velocity.normalize();
    this.velocity.mulScalar(this.speed,this.velocity);
    if (this.status != CHARACTER_STATUS.WALKING) {
      this.status = CHARACTER_STATUS.WALKING;
      this.node.setAnimation( CHARACTER_STATUS.WALKING );
      this.node.play(true);
    }
    this.node.animationSpeed = this.speed/1.5;
    var defaultVec = new gewVec2(0,1);
    var angle = defaultVec.angleFrom(this.velocity);
    if( this.velocity.x<0)
      angle = -angle;
    this.node.setRotateXYZ(0,1,0,angle* GEW_MATH_RADIAN_TO_DEGREE);
    this.physicBody.velocity.set(this.velocity.x,0.0,this.velocity.y);
  },
  moveTo:function(x,y)
  {
    var currPos = this.position;
    this.speed = CHARACTER_RUN_SPD;
    this.startPosition.set(currPos.x,currPos.y);
    this.desPosition.set(x,y);
    this.velocity = this.desPosition.sub(currPos);
    this.velocity.normalize();
    this.velocity.mulScalar(this.speed,this.velocity);
    if (this.status != CHARACTER_STATUS.RUNNING) {
      this.status = CHARACTER_STATUS.RUNNING;
      if(!this.isGirl)
        this.node.setAnimation( CHARACTER_ANIMATION.RUNNING );
      else
        this.node.setAnimation( GIRL_ANIMATION.RUNNING );
      this.node.play(true);
    }
    this.node.animationSpeed = this.speed/3;
    var defaultVec = new gewVec2(0,1);
    var angle = defaultVec.angleFrom(this.velocity);
    if( this.velocity.x<0)
      angle = -angle;
    this.node.setRotateXYZ(0,1,0,angle* GEW_MATH_RADIAN_TO_DEGREE);
    this.physicBody.velocity.set(this.velocity.x,0.0,this.velocity.y);
    
    if (this == my_player) {
      var pos = this.node.position;
      var str = "2\r\n" + this.name + "\r\n" + pos.x + ";" + pos.y + ";" + pos.z + "\r\n" + x + ";" + y;
      this.screen.socket.send(str);
    }    
  }
}
